home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / a_correlate.pro next >
Text File  |  1997-07-08  |  3KB  |  108 lines

  1. ; $Id: a_correlate.pro,v 1.8 1997/01/15 03:11:50 ali Exp $
  2. ; Copyright (c) 1995-1997, Research Systems, Inc.  All rights reserved.
  3. ;       Unauthorized reproduction prohibited.
  4. ;+
  5. ; NAME:
  6. ;       A_CORRELATE
  7. ;
  8. ; PURPOSE:
  9. ;       This function computes the autocorrelation Px(L) or autocovariance
  10. ;       Rx(L) of a sample population X as a function of the lag (L).
  11. ;
  12. ; CATEGORY:
  13. ;       Statistics.
  14. ;
  15. ; CALLING SEQUENCE:
  16. ;       Result = A_correlate(X, Lag)
  17. ;
  18. ; INPUTS:
  19. ;       X:    An n-element vector of type integer, float or double.
  20. ;
  21. ;     LAG:    A scalar or n-element vector, in the interval [-(n-2), (n-2)],
  22. ;             of type integer that specifies the absolute distance(s) between 
  23. ;             indexed elements of X.
  24. ;
  25. ; KEYWORD PARAMETERS:
  26. ;       COVARIANCE:    If set to a non-zero value, the sample autocovariance
  27. ;                      is computed.
  28. ;
  29. ;       DOUBLE:        If set to a non-zero value, computations are done in
  30. ;                      double precision arithmetic.
  31. ;
  32. ; EXAMPLE
  33. ;       Define an n-element sample population.
  34. ;         x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57]
  35. ;
  36. ;       Compute the autocorrelation of X for LAG = -3, 0, 1, 3, 4, 8
  37. ;         lag = [-3, 0, 1, 3, 4, 8]
  38. ;         result = a_correlate(x, lag)
  39. ;
  40. ;       The result should be:
  41. ;         [0.0146185, 1.00000, 0.810879, 0.0146185, -0.325279, -0.151684]
  42. ;
  43. ; PROCEDURE:
  44. ;       See computational formula published in IDL manual.
  45. ;
  46. ; REFERENCE:
  47. ;       INTRODUCTION TO STATISTICAL TIME SERIES
  48. ;       Wayne A. Fuller
  49. ;       ISBN 0-471-28715-6
  50. ;
  51. ; MODIFICATION HISTORY:
  52. ;       Written by:  GGS, RSI, October 1994
  53. ;       Modified:    GGS, RSI, August 1995
  54. ;                    Corrected a condition which excluded the last term of the
  55. ;                    time-series.
  56. ;       Modified:    GGS, RSI, April 1996
  57. ;                    Simplified AUTO_COV function. Added DOUBLE keyword.
  58. ;                    Modified keyword checking and use of double precision.
  59. ;-
  60.  
  61. FUNCTION Auto_Cov, X, M, nX, Double = Double
  62.   ;Sample autocovariance function.
  63.  
  64.   Xmean = TOTAL(X, Double = Double) / nX
  65.   RETURN, TOTAL((X[0:nX - M - 1L] - Xmean) * (X[M:nX - 1L] - Xmean), $
  66.                  Double = Double)
  67.  
  68. END
  69.  
  70. FUNCTION A_Correlate, X, Lag, Covariance = Covariance, Double = Double
  71.  
  72.   ;Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l)
  73.   ;as a function of the lag (l).
  74.  
  75.   ON_ERROR, 2
  76.  
  77.   TypeX = SIZE(X)
  78.   nX = TypeX[TypeX[0]+2]
  79.  
  80.   ;Check length.
  81.   if nX lt 2 then $
  82.     MESSAGE, "X array must contain 2 or more elements."
  83.  
  84.   ;If the DOUBLE keyword is not set then the internal precision and
  85.   ;result are identical to the type of input.
  86.   if N_ELEMENTS(Double) eq 0 then $
  87.     Double = (TypeX[TypeX[0]+1] eq 5)
  88.  
  89.   nLag = N_ELEMENTS(Lag)
  90.  
  91.   if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
  92.  
  93.   if Double eq 0 then Auto = FLTARR(nLag) else Auto = DBLARR(nLag)
  94.  
  95.   if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Autocorrelation.
  96.     for k = 0, nLag-1 do $
  97.       Auto[k] = Auto_Cov(X, ABS(Lag[k]), nX, Double = Double) / $
  98.                 Auto_Cov(X, 0L, nX, Double = Double)
  99.   endif else begin ;Compute Autocovariance.
  100.     for k = 0, nLag-1 do $ 
  101.       Auto[k] = Auto_Cov(X, ABS(Lag[k]), nX, Double = Double) / nX
  102.   endelse
  103.  
  104.   if Double eq 0 then RETURN, FLOAT(Auto) else $
  105.   RETURN, Auto
  106.  
  107. END
  108.